package com.gogowan.agroenterprise.newview;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.RetrofitUtil.GsonUtil;
import com.gogowan.agroenterprise.contorl.RetrofitUtil.JSON_Tool;
import com.gogowan.agroenterprise.contorl.RetrofitUtil.MyRetrofit;
import com.gogowan.agroenterprise.model.DH_Result;
import com.gogowan.agroenterprise.model.Dh_Request;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.Base_Adapater;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;
import u.aly.T;

import static android.content.Context.MODE_PRIVATE;

public class ListFragment extends Fragment {
    private View  view;
    private int Type;
    private ListView listView;
    private List<DH_Result.ExchangeListInfoBean> list=new ArrayList<>();
    private Base_Adapater adapater;
    
    private ProgressDialog progressDialog;
    private String OutStorageNO;
    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.activity_listview,null);
        SharedPreferences preferences = getActivity().getSharedPreferences("LoginINfo", MODE_PRIVATE);
        
        
        view.findViewById(R.id.list_title_fl).setVisibility(View.GONE);
        listView=(ListView)view.findViewById(R.id.listView_Activity_List);
        adapater=new Base_Adapater(getContext(),list);
        adapater.setOnItemListener(new Base_Adapater.OnItemListener() {
            @Override
            public String ItemStr(int position) {
                DH_Result.ExchangeListInfoBean infoBean=list.get(position);
                String val= "出库单号:"+infoBean.getOutStorageNo()+
                        "\n产品名称:"+infoBean.getMaterialName()+
                        "\n入库数量:"+infoBean.getMaCount()+
                        "\n入库垛数:"+infoBean.getDuoCount()+
                        "\n入库箱数:"+infoBean.getBoxCount();
                return val;
            }
        });
        listView.setAdapter(adapater);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getContext(),DH_InforMation_Activity.class);
                intent.putExtra("bean",list.get(position));
                intent.putExtra("OutStorageNO",OutStorageNO);
                startActivityForResult(intent,205);
            }
        });
        InitHttp();
        return view;
    }
    public void  setType(int type){
        Type=type;
    }
    public void InitHttp(){
        Dh_Request request=new Dh_Request();
//        request.setEnterpriseId(loginResultInfo.getEnterpriseinfo().getEnterpriseid());
//        request.setDealerId(loginResultInfo.getEnterpriseinfo().getDealerId());
        request.setStatus(Type);
        progressDialog = ProgressDialog.show(getContext(), getString(R.string.alert_title),
                getString(R.string.alert_message), true, false);
        Http(request,47000);
    }
    public void Http(Object o, int HttpServerCode){
        String json= GsonUtil.GsonString(o);
        Message message = new Message(HttpServerCode, true, json);
        String  jsonStr= GsonUtil.GsonString(message);
        MyRetrofit.init(getContext(),getString(R.string.first_url)).Post1(new JSON_Tool.jsonPutBuild().getRetrofitReqBodyJson(jsonStr)).enqueue(new retrofit2.Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (progressDialog!=null){
                    progressDialog.cancel();
                }
                if (!Utils.isEmpty(response.body())) {
                    Message message = (Message) Utils.fromJSON(response.body(),
                            Message.class);
                    String string = Utils.getMessageContent(message);
                    if (string == null || string.equals("")) {
                        Toast.makeText(getContext(), getString(R.string.analyse_error), Toast.LENGTH_SHORT).show();
                    } else {
                        DH_Result dh_result=GsonUtil.GsonToBean(string,DH_Result.class);
                        if (dh_result!=null){
                            if (dh_result.getResult()>0){
                                list.clear();
                                list.addAll(dh_result.getExchangeListInfo());
                                OutStorageNO=dh_result.getOutStorageNO()+"";
                                adapater.notifyDataSetChanged();
                            }else {
                                Toast.makeText(getContext(), dh_result.getResultinfo(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                } else {
                    Toast.makeText(getContext(), getString(R.string.Internet_JsonError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                if (progressDialog!=null){
                    progressDialog.cancel();
                }
                Toast.makeText(getContext(), getString(R.string.Internet_Error), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
